package fr.lille.inria.adam.lifl.EcoGardeInventory.Utils;

import java.util.Observable;
import java.util.Observer;

import org.apache.http.client.CookieStore;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.protocol.HttpContext;

import android.util.Log;
import fr.lille.inria.adam.lifl.EcoGardeInventory.Tasks.HttpAsyncTask;
import fr.lille.inria.adam.lifl.EcoGardeInventory.Tasks.LoginTask;
import fr.lille.inria.adam.lifl.EcoGardeInventory.Tasks.RetreivePrecisionTask;
import fr.lille.inria.adam.lifl.EcoGardeInventory.Tasks.RetrieveCommunesTask;
import fr.lille.inria.adam.lifl.EcoGardeInventory.Tasks.RetrieveCommunsTask;
import fr.lille.inria.adam.lifl.EcoGardeInventory.Tasks.RetrieveComportementsTask;
import fr.lille.inria.adam.lifl.EcoGardeInventory.Tasks.RetrieveEntitesTask;
import fr.lille.inria.adam.lifl.EcoGardeInventory.Tasks.RetrieveGroupesEspecesTask;
import fr.lille.inria.adam.lifl.EcoGardeInventory.Tasks.RetrieveInfosCompTask;
import fr.lille.inria.adam.lifl.EcoGardeInventory.Tasks.RetrieveLatinsTask;
import fr.lille.inria.adam.lifl.EcoGardeInventory.Tasks.RetrieveObservateursTask;
import fr.lille.inria.adam.lifl.EcoGardeInventory.Tasks.RetrievePeriodesLibresTask;
import fr.lille.inria.adam.lifl.EcoGardeInventory.Tasks.RetrieveRecensementsTask;
import fr.lille.inria.adam.lifl.EcoGardeInventory.Tasks.RetrieveSitesTask;
import fr.lille.inria.adam.lifl.EcoGardeInventory.Tasks.RetrieveTerritoiresTask;
import fr.lille.inria.adam.lifl.EcoGardeInventory.Tasks.SendObservationTask;

public class ClientSIG {

	private final String TAG = "ClientSIG";
	protected Browser browser = new Browser();

	protected User ownerProfile = null;

	public User getUserProfile() {
		return this.ownerProfile;
	}

	public ClientSIG() {
	}

	private void prepareTask(HttpAsyncTask<?, ?> task, Observer onResult) {
		task.addObserver(onResult);
	}

	public void closed() {
		browser.client.close();
	}

	public HttpContext getContext() {
		return browser.getContext();
	}

	public void setContext(HttpContext context) {
		browser.setContext(context);
	}

	public void executeLogin(final String login, final String password,
			final Observer onResult) {

		CookieStore cs = (CookieStore) browser.getContext().getAttribute(
				ClientContext.COOKIE_STORE);

		for (int i = 0; i < cs.getCookies().size(); i++) {

			Log.d(TAG, "Cookies from the Browser Context: "
					+ cs.getCookies().get(i));
		}
		LoginTask task = new LoginTask();
		task.setBrowser(browser);

		// God a besoin du resultat de cette tache
		final Observer proxyObserver = new Observer() {
			public void update(Observable o, Object arg) {
				Integer code = (Integer) arg;
				if (code == LoginTask.SUCCES_CODE)
					ownerProfile = new User(null, 0);
				else
					ownerProfile = null;

				// allez, on fais la passe :)
				onResult.update(o, arg);
			}
		};

		prepareTask(task, proxyObserver);
		task.execute(login, password);
	}

	public void retrieveObservateurs(Observer onResult) {
		RetrieveObservateursTask task = new RetrieveObservateursTask();
		task.setBrowser(browser);
		prepareTask(task, onResult);
		task.execute();
	}

	public void retrieveTerritoires(Observer onResult) {
		RetrieveTerritoiresTask task = new RetrieveTerritoiresTask();
		task.setBrowser(browser);
		prepareTask(task, onResult);
		task.execute();
	}

	public void retrieveSites(String code_territoire, Observer onResult) {
		RetrieveSitesTask task = new RetrieveSitesTask();
		task.setBrowser(browser);
		prepareTask(task, onResult);
		task.execute(code_territoire);
	}

	public void retrieveEntites(String code_site, Observer onResult) {
		RetrieveEntitesTask task = new RetrieveEntitesTask();
		task.setBrowser(browser);
		prepareTask(task, onResult);
		task.execute(code_site);
	}

	public void retrieveCommunes(Observer onResult) {
		RetrieveCommunesTask task = new RetrieveCommunesTask();
		task.setBrowser(browser);
		prepareTask(task, onResult);
		task.execute();
	}

	public void retrieveGroupesEspeces(Observer onResult) {
		RetrieveGroupesEspecesTask task = new RetrieveGroupesEspecesTask();
		task.setBrowser(browser);
		prepareTask(task, onResult);
		task.execute();
	}

	public void retrieveRecensements(Observer onResult) {
		RetrieveRecensementsTask task = new RetrieveRecensementsTask();
		task.setBrowser(browser);
		prepareTask(task, onResult);
		task.execute();
	}

	public void retrievePeriodesLibres(Observer onResult) {
		RetrievePeriodesLibresTask task = new RetrievePeriodesLibresTask();
		task.setBrowser(browser);
		prepareTask(task, onResult);
		task.execute();
	}

	public void retrieveNomsCommuns(String code_groupe_espece, Observer onResult) {
		RetrieveCommunsTask task = new RetrieveCommunsTask();
		task.setBrowser(browser);
		prepareTask(task, onResult);
		task.execute(code_groupe_espece);
	}

	public void retrieveNomsLatins(String code_groupe_espece, Observer onResult) {
		RetrieveLatinsTask task = new RetrieveLatinsTask();
		task.setBrowser(browser);
		prepareTask(task, onResult);
		task.execute(code_groupe_espece);
	}

	public void retrievePrecisionEffectif(String code_groupe_espece,
			Observer onResult) {
		RetreivePrecisionTask task = new RetreivePrecisionTask();
		task.setBrowser(browser);
		prepareTask(task, onResult);
		task.execute(code_groupe_espece);
	}

	public void retrieveComportements(String code_groupe_espece,
			Observer onResult) {
		RetrieveComportementsTask task = new RetrieveComportementsTask();
		task.setBrowser(browser);
		prepareTask(task, onResult);
		task.execute(code_groupe_espece);
	}

	public void sendObservation(Note n, Observer onResult) {
		SendObservationTask task = new SendObservationTask();
		task.setBrowser(browser);
		prepareTask(task, onResult);
		task.execute(n);
	}

	public void retrieveInfosComp(String code_groupe_espece, String id,
			Observer onResult) {
		RetrieveInfosCompTask task = new RetrieveInfosCompTask();
		task.setBrowser(browser);
		prepareTask(task, onResult);
		task.execute(code_groupe_espece, id);
	}
}
